package com.skcc.ts.nsi.service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.telephony.SmsMessage;
import android.util.Log;

import com.skcc.ts.nsi.MEIntro;
import com.skcc.ts.nsi.vo.MESmsVo;

public class SMSReceiver extends BroadcastReceiver {

	protected static final String LOG_TAG = "SMSReceiver";

	private static final String ACTION = "android.provider.Telephony.SMS_RECEIVED";

	@Override
	public void onReceive(Context context, Intent intent) {
		Log.i(LOG_TAG, "[inside onReceive]");
		SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(context);
		if (intent.getAction().equals(ACTION)) {
			StringBuilder sb = new StringBuilder();
			Bundle bundle = intent.getExtras();

			String number = "";
			String message = "";
			String temp = "";
			String dateTemp = "";
			String timeTemp = "";
			SimpleDateFormat formatter = new SimpleDateFormat("yyyy", Locale.KOREA);
			Date currentTime = new Date();
			MESmsVo smsVo = new MESmsVo();

			if (bundle != null) {
				Object[] pdusObj = (Object[]) bundle.get("pdus");
				SmsMessage[] msgs = new SmsMessage[pdusObj.length];
				for (int i = 0; i < pdusObj.length; i++) {
					msgs[i] = SmsMessage.createFromPdu((byte[]) pdusObj[i]);
					number = msgs[i].getOriginatingAddress();
					message = msgs[i].getMessageBody();
					smsVo.cardNum = pref.getString("card_num", "");
					if (message.indexOf("(" + smsVo.cardNum + ")") != -1) {
						smsVo.message = message;
						String[] dateSplit = message.split("[ ]");
						temp = dateSplit[1];
						timeTemp = dateSplit[2];
						dateSplit = temp.split("/");
						dateTemp = dateSplit[0] + dateSplit[1];
						dateSplit = timeTemp.split(":");
						dateSplit[1] = dateSplit[1].substring(0, 2);
						timeTemp = dateSplit[0] + dateSplit[1];
						smsVo.date = formatter.format(currentTime) + dateTemp + timeTemp;
						Log.d("date",smsVo.date);
						
						String[] strSplit = message.split("[/]");
						temp = strSplit[2].substring(0, strSplit[2].length() - 1);
						temp.trim();
						Log.d("temp",temp);
						
						String[] subSplit = temp.split("[,]");
						smsVo.cost = "";
						
						for (int j = 0; j < subSplit.length; j++) {
							if (subSplit[j].toString() != null)
								smsVo.cost = smsVo.cost + subSplit[j];
						}
						Log.d("cost",smsVo.cost);
						
						smsVo.distributor = strSplit[4];
						
						this.abortBroadcast();
						Intent sendIntent = new Intent(context, MEIntro.class);
						sendIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

						sendIntent.putExtra("SMS", true);
						sendIntent.putExtra("message", smsVo.message);
						sendIntent.putExtra("date", smsVo.date.trim());
						sendIntent.putExtra("card", smsVo.cardNum.trim());
						sendIntent.putExtra("distributor", smsVo.distributor.trim());
						sendIntent.putExtra("cost", smsVo.cost.trim());
						sendIntent.putExtra("phonenum", number.trim());
						context.startActivity(sendIntent);
					}

					/*
					 * int j=0; int idx=0; int lastIdx=0; int startIdx=0; int endIdx=0; String srch = "/";
					 * msgs[i]=SmsMessage.createFromPdu((byte[])pdusObj[i]); number=msgs[i].getOriginatingAddress();
					 * message=msgs[i].getMessageBody().toString(); if(message.indexOf("("+"1997"+")")!=-1){
					 * while((idx=message.indexOf(srch,lastIdx))!=-1){ j++; lastIdx=idx+1; if(j==2){ startIdx=idx+2; }else if(j==3) }
					 * 
					 * }
					 */
				}

				Log.i(LOG_TAG, "[SMSApp Bundle] " + bundle.toString());
			}

			/*
			 * PendingIntent pi = PendingIntent.getActivity(context, 0, sendIntent, 0); try { pi.send(); } catch (Exception e) {
			 * e.printStackTrace(); }
			 */
		}

	}
}
